package hbase.HbaseStudy;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.util.List;


public class Hb05_getData {
    private static String tableName = "sccc01:t3";
    private static Configuration conf;
    private static Connection HConn;
    private static Admin admin;
    private static Table table;

    public static void main(String[] args) throws IOException {
        conf = HBaseConfiguration.create();
        HConn = ConnectionFactory.createConnection(conf);
        admin = HConn.getAdmin();
        table = HConn.getTable(TableName.valueOf(tableName));

        try {
            /*
             # 获取指定行中所有列的数据信息
                get '表名','行键'
             # 获取指定行中指定列族下所有列的数据信息
                get '表名','行键','列族'
             # 获取指定行中指定列的数据信息
                get'表名','行键','列族:列名'
            */
            Get get = new Get("02".getBytes());
            /*
            查看指定列柱和指定列名的数据
            get.addColumn("info".getBytes(),"name".getBytes());
            */
            Result result = table.get(get); // 一个result就是一行数据
            // 处理数据集
            List<Cell> cells = result.listCells(); // 获取一行所有单元格
            for (Cell cell : cells) {
                // 获取一个单元格可以获取所有内容

                byte[] cloneRow = CellUtil.cloneRow(cell);
                String rowKey = Bytes.toString(cloneRow); // 将字节数据转化为字符窜

                byte[] cloneFamily = CellUtil.cloneFamily(cell);
                String family = Bytes.toString(cloneFamily);

                byte[] cloneQualifier = CellUtil.cloneQualifier(cell);
                String qualifier = Bytes.toString(cloneQualifier);

                byte[] value = CellUtil.cloneValue(cell);
                String values = Bytes.toString(value);

                System.out.println("行间："+rowKey+" 列柱："+family+" 列名："+qualifier+" 值："+values);
            }
            System.out.println("ok-----------------");
        } finally {
            // 释放资源
            admin.close();
            HConn.close();
        }
    }
}
